import os
from flask import Blueprint, send_from_directory
from ..extentions import app, Api
from .react_app_view import ReactAppView

dist_dir = os.path.abspath(os.path.join(app.root_path, '..', '..', '..', 'web', 'dist'))
react_app_blue = Blueprint('react_app', __name__)

@react_app_blue.route('/<path:path>')
def serve_static(path):
    if '.' in path:
        response = send_from_directory(dist_dir, path)
        if path.endswith('.js'):
            response.headers['Content-Type'] = 'application/javascript'
            if 'module' in path or path.endswith('.mjs'):
                response.headers['Content-Type'] = 'application/javascript; charset=utf-8'
        return response
    return send_from_directory(dist_dir, 'index.html')

react_app_api = Api(react_app_blue, prefix='')
react_app_api.add_resource(ReactAppView, '/')
